package com.analog.vserver.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;

import org.zeroturnaround.exec.ProcessExecutor;

import lombok.extern.java.Log;

/**
 * FFMPEG 的相关操作
 *
 * @author Administrator
 */
@Log
public class FFMpegUtil {
	
	private static String temVideoAbsPath = "/home/tem_video";
	
	private FFMpegUtil() {
	}
	
	private static String OS = System.getProperty("os.name").toLowerCase();

	public static boolean isWindows(){
        return OS.indexOf("windows")>=0;
    }
	public static void convetorToTs(String videoInputPath, String videoOutPath)
			throws IOException, InterruptedException, TimeoutException {
		if(isWindows()) {
			convetorToTsByWindows(videoInputPath, videoOutPath);
		}else {
			convetorToTsByDocker(videoInputPath, videoOutPath);
		}
	}

	public static void sliceTsToM3U8(String tsPath, String m3u8Path)
			throws IOException, InterruptedException, TimeoutException {
		if(isWindows()) {
			sliceTsToM3U8ByWindows(tsPath, m3u8Path);
		}else {
			sliceTsToM3U8ByDocker(tsPath, m3u8Path);
		}
	}

	private static void convetorToTsByDocker(String videoInputPath, String videoOutPath)
			throws IOException, InterruptedException, TimeoutException {
//    	docker run -v /home/minio/data:/home/minio/data jrottenberg/ffmpeg:vaapi -y -i 
//    	/home/minio/data/test/1fb54a09b77a54a58dfad571492516a0.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb /home/minio/data/test/1fb54a09b77a54a58dfad571492516a0.ts
		List<String> command = new ArrayList<>();
//		command.add("docker");
//		command.add("run");
//		command.add("-v");
//		command.add(temVideoAbsPath + ":/tem_video");
//		command.add("jrottenberg/ffmpeg:vaapi");
		command.add("ffmpeg");
		command.add("-y");
		command.add("-i");
		command.add(videoInputPath);
		command.add("-vcodec");
		command.add("copy");
		command.add("-acodec");
		command.add("copy");
		command.add("-vbsf");
		command.add("h264_mp4toannexb");
		command.add(videoOutPath);
		log.info("command : " + Arrays.toString(command.toArray()));
		String output = new ProcessExecutor().command(command).readOutput(true).execute().outputUTF8();
		log.info(output);
	}

	public static void sliceTsToM3U8ByDocker(String tsPath, String m3u8Path)
			throws IOException, InterruptedException, TimeoutException {
//      docker run -v /home/minio/data:/home/minio/data jrottenberg/ffmpeg:vaapi -i /home/minio/data/test/1fb54a09b77a54a58dfad571492516a0.ts 
//    	-c copy -map 0 -f segment -segment_list /home/minio/data/test/video/index.m3u8 -segment_time 2 /home/minio/data/test/video/nxb-%04d.ts
		List<String> command = new ArrayList<>();
//		command.add("docker");
//		command.add("run");
//		command.add("-v");
//		command.add(temVideoAbsPath + ":/tem_video");
//		command.add("jrottenberg/ffmpeg:vaapi");
		command.add("ffmpeg");
		command.add("-i");
		command.add(tsPath);
		command.add("-c");
		command.add("copy");
		command.add("-map");
		command.add("0");
		command.add("-f");
		command.add("segment");
		command.add("-segment_list");
		command.add(m3u8Path + File.separator + "index.m3u8");
		command.add("-segment_time");
		command.add("2");
		command.add(m3u8Path + File.separator + "nxb-%04d.ts");
		log.info("command : " + Arrays.toString(command.toArray()));
		String output = new ProcessExecutor().command(command).readOutput(true).execute().outputUTF8();
		log.info(output);
	}
	
	private static void convetorToTsByWindows(String videoInputPath, String videoOutPath)
			throws IOException, InterruptedException, TimeoutException {
//    	docker run -v /home/minio/data:/home/minio/data jrottenberg/ffmpeg:vaapi -y -i 
//    	/home/minio/data/test/1fb54a09b77a54a58dfad571492516a0.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb /home/minio/data/test/1fb54a09b77a54a58dfad571492516a0.ts
		List<String> command = new ArrayList<>();
		command.add("ffmpeg");
		command.add("-y");
		command.add("-i");
		command.add(videoInputPath);
		command.add("-vcodec");
		command.add("copy");
		command.add("-acodec");
		command.add("copy");
		command.add("-vbsf");
		command.add("h264_mp4toannexb");
		command.add(videoOutPath);
		log.info("command : " + Arrays.toString(command.toArray()));
		String output = new ProcessExecutor().command(command).readOutput(true).execute().outputUTF8();
		log.info(output);
	}

	public static void sliceTsToM3U8ByWindows(String tsPath, String m3u8Path)
			throws IOException, InterruptedException, TimeoutException {
//      docker run -v /home/minio/data:/home/minio/data jrottenberg/ffmpeg:vaapi -i /home/minio/data/test/1fb54a09b77a54a58dfad571492516a0.ts 
//    	-c copy -map 0 -f segment -segment_list /home/minio/data/test/video/index.m3u8 -segment_time 2 /home/minio/data/test/video/nxb-%04d.ts
		List<String> command = new ArrayList<>();
		command.add("ffmpeg");
		command.add("-i");
		command.add(tsPath);
		command.add("-c");
		command.add("copy");
		command.add("-map");
		command.add("0");
		command.add("-f");
		command.add("segment");
		command.add("-segment_list");
		command.add(m3u8Path + File.separator + "index.m3u8");
		command.add("-segment_time");
		command.add("2");
		command.add(m3u8Path + File.separator + "nxb-%04d.ts");
		log.info("command : " + Arrays.toString(command.toArray()));
		String output = new ProcessExecutor().command(command).readOutput(true).execute().outputUTF8();
		log.info(output);
	}
	public static String getTemVideoAbsPath() {
		return temVideoAbsPath;
	}
	public static void setTemVideoAbsPath(String temVideoAbsPath) {
		FFMpegUtil.temVideoAbsPath = temVideoAbsPath;
	}

}
